import axios from 'axios';
export function httpJson(method: string, url: string, params = {}) {
    return new Promise((resolve, reject) => {
      let config: any = {};
      if (method === 'get' || method === 'GET' || method === 'delete' || method === 'DELETE') {
        config = { params, method, url };
      } else {
        config = { data: params, method, url };   // post请求中的数据放在data里面
      }
      axios(config).then(response => {
        resolve(response);
      }).catch(error => {
        reject(error);
      })
    })
} 

export function post(url:string,params:{}) {
    return httpJson('post',url,params)
}

export function put(url:string,params:{}) {
    return httpJson('put',url,params)
}

export function get(url:string,params:{}) {
    return new Promise((resolve,reject)=>{
        axios({
            params,
            method:'get',
            url
        }).then(response=>{
            resolve(response);
        }).catch(error=>{
            reject(error);
        })
    })
}

export function del(url:string,params:{}) {
    return new Promise((resolve,reject)=>{
        axios({
            params,
            method:'delete',
            url
        }).then(response=>{
            resolve(response);
        }).catch(error=>{
            reject(error);
        })
    })
}
